package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.HozomDetailAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.HozmDetailEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.notchtool.NotchTools;
import com.runen.infinitemovement.tool.notchtool.core.NotchProperty;
import com.runen.infinitemovement.tool.notchtool.core.OnNotchCallBack;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HozomDetailActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.btn_finish)
    Button btnFinish;
    @BindView(R.id.rv_hozom_details_list)
    RecyclerView rvHozomDetailsList;
    @BindView(R.id.tv_hozom_num)
    TextView tvHozomNum;
    @BindView(R.id.rl_title_content)
    RelativeLayout rl_title_content;
    private HozomDetailAdapter detailAdapter;
    private int page = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hozom_detail;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        srl.setOnRefreshListener(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvHozomDetailsList.setLayoutManager(layoutManager);
        detailAdapter = new HozomDetailAdapter(this);
        detailAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvHozomDetailsList.setAdapter(detailAdapter);

        //适配刘海屏
        NotchTools.getFullScreenTools().translucentStatusBar(this, new OnNotchCallBack() {
            @Override
            public void onNotchPropertyCallback(NotchProperty notchProperty) {
                int height = CommonUtil.getStatusBarHeight(HozomDetailActivity.this);
                rlTitle.setPadding(0, height, 0, 0);
            }
        });

        initData();
        loadMore();
    }

    private void loadMore() {
        detailAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreInfo();
            }
        });
        detailAdapter.getLoadMoreModule().setAutoLoadMore(false);
        detailAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void loadMoreInfo() {
        String string = "?limit=20&page=%s";
        new UserAPI().hozomDetail(String.format(string, page), new BaseResultCallback<HozmDetailEntity>() {
            @Override
            public void onResponse(HozmDetailEntity response) {
                if (response.data != null) {
                    if (response.data.pageDTO != null) {
                        if (response.data.pageDTO.data != null && response.data.pageDTO.data.size() > 0) {
                            detailAdapter.addData(response.data.pageDTO.data);
                            if (detailAdapter.getData().size() > +response.data.pageDTO.count) {
                                detailAdapter.getLoadMoreModule().loadMoreEnd(true);
                                detailAdapter.getLoadMoreModule().setEnableLoadMore(false);
                            } else {
                                page++;
                                detailAdapter.getLoadMoreModule().loadMoreComplete();
                                detailAdapter.getLoadMoreModule().setEnableLoadMore(true);
                            }
                        } else {
                            detailAdapter.getLoadMoreModule().loadMoreEnd(true);
                            detailAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                }
            }
        });
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=20&page=1";
        new UserAPI().hozomDetail(string, new BaseResultCallback<HozmDetailEntity>() {
            @Override
            public void onResponse(HozmDetailEntity response) {
                srl.setRefreshing(false);
                tvHozomNum.setText(String.valueOf(response.data.total));
                if (response.data != null) {
                    if (response.data.pageDTO != null) {
                        if (response.data.pageDTO.data != null && response.data.pageDTO.data.size() > 0) {
                            detailAdapter.setNewInstance(response.data.pageDTO.data);
                            if (detailAdapter.getData().size() > +response.data.pageDTO.count) {
                                detailAdapter.getLoadMoreModule().loadMoreEnd(true);
                                detailAdapter.getLoadMoreModule().setEnableLoadMore(false);
                            }
                        }
                    }
                }
            }
        });
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @OnClick({R.id.btn_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }
}